![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@thi.ng/porter-duff
Advanced tools
Porter-Duff operators for packed ints & float-array alpha compositing
[!NOTE] This is one of 201 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.
🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️
Porter-Duff operators for packed ints & float-array alpha compositing.
This package provides all 13 fundamental Porter-Duff compositing / blending operators, and utilities to pre/post-multiply alpha. All operators are available for packed ARGB/ABGR 32bit packed ints or RGBA float vectors.
STABLE - used in production
Search or submit any issues for this package
yarn add @thi.ng/porter-duff
ESM import:
import * as pd from "@thi.ng/porter-duff";
Browser ESM import:
<script type="module" src="https://esm.run/@thi.ng/porter-duff"></script>
For Node.js REPL:
const pd = await import("@thi.ng/porter-duff");
Package sizes (brotli'd, pre-treeshake): ESM: 1.03 KB
Note: @thi.ng/api is in most cases a type-only import (not used at runtime)
Two projects in this repo's /examples directory are using this package:
Screenshot | Description | Live demo | Source |
---|---|---|---|
![]() | Pixel buffer manipulations | Demo | Source |
![]() | Port-Duff image compositing / alpha blending | Demo | Source |
import * as pd from "@thi.ng/porter-duff";
// packed int version (premultiplied ARGB)
pd.SRC_OVER_I(0x80800000, 0xcc0cc00)
// automatically premultiply inputs & post-multiply result
pd.porterDuffPInt(pd.SRC_OVER_I, 0x80ff0000, 0xcc00ff00);
// the above is same as:
pd.postmultiplyInt(
pd.SRC_OVER_I(
pd.premultiplyInt(0x80ff0000),
pd.premultiplyInt(0xcc00ff00)
)
)
// premultiplied float version [R,G,B,A]
pd.SRC_OVER_F([1, 0, 0, 0.5], [0, 1, 0, 0.8]);
Integer operators are suffixed with _I
, float versions with _F
.
Consult above diagram for expected results.
CLEAR
SRC
DEST
SRC_OVER
DEST_OVER
SRC_IN
DEST_IN
SRC_OUT
DEST_OUT
SRC_ATOP
DEST_ATOP
XOR
PLUS
New operators (e.g. for blend modes) can be easily defined via
porterDuff
/ porterDuffInt
. Both functions take 2 function arguments
to extract blend coefficients from the src & dest colors:
import { porterDuffInt } from "@thi.ng/porter-duff";
// coefficient functions take the normalized alpha values
// of both colors as arguments, but unused here...
const customOp = porterDuffInt(() => -0.5, () => 1);
The following coefficient functions are included by default (and are used by all standard operators):
ZERO
=> 0ONE
=> 1A
=> alpha of src colorB
=> alpha of dest colorONE_MINUS_A
=> 1 - alpha of src colorONE_MINUS_B
=> 1 - alpha of dest colorThe following modifiers are also discussed in the original Porter-Duff paper (linked above).
darken
/ darkenInt
dissolve
/ dissolveInt
opacity
/ opacityInt
All Porter-Duff operators expect colors with pre-multiplied alpha. Premultiplication is also recommended for transparent WebGL textures (especially when using mipmaps). For that purpose the following helpers might be useful:
premultiply
/ premultiplyInt
postmultiply
/ postmultiplyInt
isPremultiplied
/ isPremultipliedInt
Furthermore, existing PD operators can be wrapped with automatic
pre/post-multiplies using porterDuffP
/ porterDuffPInt
(see example
above).
Note: HTML Canvas ImageData
is using non-premultiplied colors.
If this project contributes to an academic publication, please cite it as:
@misc{thing-porter-duff,
title = "@thi.ng/porter-duff",
author = "Karsten Schmidt",
note = "https://thi.ng/porter-duff",
year = 2018
}
© 2018 - 2025 Karsten Schmidt // Apache License 2.0
FAQs
Porter-Duff operators for packed ints & float-array alpha compositing
We found that @thi.ng/porter-duff demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.